package com.car.service;

import com.car.dao.DB;
import com.car.entyti.Carpool;
import com.car.entyti.User;

import javax.servlet.http.HttpServlet;
import java.util.List;
import java.util.Map;

public class UserService {

    /**
     * 如果登录成功，返回用户信息，失败返回null)
     *
     * @param acc
     * @param pwd
     * @return
     */
    public User login(String acc, String pwd) {
        List<Map<String, Object>> result = DB.executeQuery("select * from t_User where account =? and password =?", acc, pwd);
        User user = null;
        if (result != null) {
            for (Map<String, Object> temp : result) {
                user = new User();
                user.setId((Integer) temp.get("id"));
                user.setAccount((String) temp.get("account"));
                user.setName((String) temp.get("name"));

            }
        }
        return user;
    }


    public User register(User user) {
        try {
            int a = DB.executeUpdate("insert into t_User(name,account,password,phone,sex,type)" +
                            "value (?,?,?,?,?,?)",
                    user.getName(),
                    user.getAccount(),
                    user.getPassword(),
                    user.getPhone(),
                    user.getSex(),
                    user.getType());

            if (a == -1) {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }


        return user;
    }

    public Carpool publish(Carpool carpool) {
        try {
            int a = DB.executeUpdate("insert into t_carpool (becity,decity,deptime,num_people,chek_people,motor_type,cost,remarks,usertype,operation)" +
                            "value (?,?,?,?,?,?,?,?,?,?)",
                    carpool.getBecity(),
                    carpool.getDecity(),
                    carpool.getDeptime(),
                    carpool.getNum_people(),
                    carpool.getChek_people(),
                    carpool.getMotor_type(),
                    carpool.getCost(),
                    carpool.getRemarks(),
                    carpool.getUsertype(),
                    carpool.getOperation()
            );


            if (a == -1) {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return carpool;
    }

    public void setToken(Integer userId, String token) {
        DB.executeUpdate("update t_User set token = ? where id = ?", token, userId);
    }

    public User getUserByToken(String token) {
        List<Map<String, Object>> result = DB.executeQuery("select * from t_User where token=?", token);
        User user = null;
        if (result != null) {
            for (Map<String, Object> temp : result) {
                user = new User();
                user.setId((Integer) temp.get("id"));
                user.setAccount((String) temp.get("account"));
                user.setName((String) temp.get("name"));
                user.setSex((String) temp.get("sex"));
                user.setPhone((String) temp.get("phone"));

            }
        }
        return user;
    }

    public User updateUser(User user) {
        DB.executeUpdate("UPDATE `t_user` SET " +
                "`name` = ?, " +
                "`account` = ?, " +
                "`password` = ?, " +
                "`phone` = ?," +
                " `sex` = ?, " +
                "`type` = ? " +
                " WHERE `id` = ?;",
                user.getName(),
                user.getAccount(),
                user.getPassword(),
                user.getPhone(),
                user.getSex(),
                user.getType(),
                user.getId());
                 return (user);
    }


}
